/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.cloud.oauthcenter.controller;

import com.lvyh.lightframe.cloud.common.model.PageResult;
import com.lvyh.lightframe.cloud.common.model.Result;
import com.lvyh.lightframe.cloud.common.model.SysOauthClient;
import com.lvyh.lightframe.cloud.oauthcenter.service.SysOauthClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/oauth-clients")
@Slf4j
public class OauthClientController {

    @Autowired
    private SysOauthClientService sysOauthClientService;

    @GetMapping("/list")
    public PageResult<SysOauthClient> queryListByPage(@RequestParam Map<String, Object> params) throws Exception {
        return sysOauthClientService.queryListByPage(params);
    }

    @GetMapping("/{clientId}")
    public SysOauthClient detail(@PathVariable String clientId) throws Exception {
        return sysOauthClientService.getByClientId(clientId);
    }

    @PostMapping("/add")
    public void add(@RequestBody SysOauthClient sysOauthClient) throws Exception {
        sysOauthClientService.add(sysOauthClient);
    }


    @PutMapping("/update")
    public void updateSysUser(@RequestBody SysOauthClient sysOauthClient) throws Exception {
        sysOauthClientService.updateById(sysOauthClient);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") String ids) throws Exception {
        List<String> list = Arrays.asList(ids.split(","));
        for (String id : list) {
            sysOauthClientService.deleteOauthClientDetails(Integer.parseInt(id));
        }
        return Result.succeed("删除成功");
    }

}
